//
//  WebViewController.m
//  CurriculumFactory
//
//  Created by apple on 2020/3/24.
//

#import "WebViewController.h"
#import <WebKit/WebKit.h>
//WKScriptMessageHandler 代理
@interface WebViewController ()<WKUIDelegate,WKNavigationDelegate>
@property (nonatomic, strong)WKWebView *webView;
@property (nonatomic, strong) UIProgressView *progressView;
@property(nonatomic,strong)UIView * baseView;
@end

@implementation WebViewController
- (instancetype)init
{
    self = [super init];
    if (self) {
        _isPush=YES;
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
     self.navigationItem.leftBarButtonItem=[UIFactory create_Nav_itemWithTitle:@"" imageName:@"icon_back_24_black" TitleColor:nil target:self action:@selector(leftNavItemClicked)];
    [self.view addSubview:self.webView];
       [self.view addSubview:self.progressView];
       [self.webView addObserver:self forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionNew context:nil];
       [self.webView addObserver:self forKeyPath:@"title" options:NSKeyValueObservingOptionNew context:NULL];
       NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:self.urlStr]];
       [self.webView loadRequest:request];
       NSLog(@"self.urlStr %@",self.urlStr);

//    [self.webView loadHTMLString:self.urlStr baseURL:nil];
}
- (WKWebView *)webView {
    if (_webView==nil) {
        WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init];
//        //初始化偏好设置属性：preferences
        config.preferences = [WKPreferences new];
        //The minimum font size in points default is 0;
        config.preferences.minimumFontSize = 10;
        //是否支持JavaScript
        config.preferences.javaScriptEnabled = YES;
        //支持小窗播放视频
        config.allowsInlineMediaPlayback = YES;

       // 不通过用户交互，是否可以打开窗口
        config.preferences.javaScriptCanOpenWindowsAutomatically = NO;
//        //通过JS与webView内容交互
//        config.userContentController = [[WKUserContentController alloc] init];
//         [config.userContentController addScriptMessageHandler:self name:JsMethodName];
//        CGFloat height = SCREEN_HEIGHT-NavigationContentTopConstant -BottomSafeHeight+50;
        _webView = [[WKWebView alloc] initWithFrame:self.view.bounds configuration:config];
//        _webView.scrollView.contentInset=UIEdgeInsetsMake(-50, 0, 0, 0);
//        [_webView setNavigationDelegate:self];
//        [_webView setUIDelegate:self];
    }
    return _webView;
}
- (UIProgressView *)progressView {
    if (_progressView==nil) {
       _progressView = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleDefault];
        [_progressView setTrackTintColor:[UIColor colorWithWhite:1.0f alpha:0.0f]];
        [_progressView setFrame:CGRectMake(0, NavigationContentTopConstant, SCREEN_WIDTH, 1)];
        [_progressView setTintColor:theme_color];
    }
    return _progressView;
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context {
    if ([keyPath isEqualToString:@"estimatedProgress"]) {
        self.progressView.progress = self.webView.estimatedProgress;
        if (self.progressView.progress == 1) {
            __weak typeof (self)weakSelf = self;
            [UIView animateWithDuration:0.25f delay:0.3f options:UIViewAnimationOptionCurveEaseOut animations:^{
                weakSelf.progressView.transform = CGAffineTransformMakeScale(1.0f, 1.4f);
            } completion:^(BOOL finished) {
                weakSelf.progressView.hidden = YES;
            }];
        }
    }else if ([keyPath isEqualToString:@"title"]) {
        if (object == self.webView){
//            self.title = self.webView.title;
        }
    } else{
        [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
    }
}
-(void)dealloc {
 //注意！注意！注意：上面将当前ViewController设置为MessageHandler之后需要在当前ViewController销毁前将其移除，否则会造成内存泄漏。
//    [self.webView.configuration.userContentController removeScriptMessageHandlerForName:JsMethodName];
    [self.webView removeObserver:self forKeyPath:@"estimatedProgress"];
    [self.webView removeObserver:self forKeyPath:@"title"];
}
-(void)leftNavItemClicked {
    if (self.isPush) {
        [self.navigationController popViewControllerAnimated:YES];
        return;
    }
    [self dismissViewControllerAnimated:YES completion:nil];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
